; ModuleID = 'get_data1.ll'
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
target triple = "i386-pc-linux-gnu"

%struct.MPI_Status = type { i32, i32, i32, i32, i32 }
%struct.ompi_communicator_t = type opaque
%struct.ompi_datatype_t = type opaque
%struct.ompi_predefined_communicator_t = type opaque
%struct.ompi_predefined_datatype_t = type opaque

@ompi_mpi_comm_world = external global %struct.ompi_predefined_communicator_t
@ompi_mpi_float = external global %struct.ompi_predefined_datatype_t
@.str = private constant [38 x i8] c"With n = %d trapezoids, our estimate\0A\00", align 4
@.str1 = private constant [36 x i8] c"of the integral from %f to %f = %f\0A\00", align 4
@ompi_mpi_int = external global %struct.ompi_predefined_datatype_t
@.str2 = private constant [18 x i8] c"Enter a, b, and n\00", align 1
@.str3 = private constant [9 x i8] c"%f %f %d\00", align 1

define i32 @main(i32 %argc, i8** %argv) nounwind {
entry:
  %argc_addr = alloca i32, align 4
  %argv_addr = alloca i8**, align 4
  %my_rank = alloca i32
  %p = alloca i32
  %a = alloca float
  %b = alloca float
  %n = alloca i32
  %integral = alloca float
  %status = alloca %struct.MPI_Status
  %"alloca point" = bitcast i32 0 to i32
  store i32 %argc, i32* %argc_addr
  store i8** %argv, i8*** %argv_addr
  %0 = call i32 @MPI_Init(i32* %argc_addr, i8*** %argv_addr) nounwind
  %1 = call i32 @MPI_Comm_rank(%struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), i32* %my_rank) nounwind
  %2 = call i32 @MPI_Comm_size(%struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), i32* %p) nounwind
  %3 = load i32* %p, align 4
  %4 = load i32* %my_rank, align 4
  call void @Get_data1(float* %a, float* %b, i32* %n, i32 %4, i32 %3) nounwind
  %5 = load float* %b, align 4
  %6 = load float* %a, align 4
  %7 = fsub float %5, %6
  %8 = load i32* %n, align 4
  %9 = sitofp i32 %8 to float
  %10 = fdiv float %7, %9
  %11 = load i32* %n, align 4
  %12 = load i32* %p, align 4
  %13 = sdiv i32 %11, %12
  %14 = load i32* %my_rank, align 4
  %15 = mul nsw i32 %14, %13
  %16 = sitofp i32 %15 to float
  %17 = fmul float %16, %10
  %18 = load float* %a, align 4
  %19 = fadd float %17, %18
  %20 = sitofp i32 %13 to float
  %21 = fmul float %20, %10
  %22 = fadd float %21, %19
  %23 = call float @Trap(float %19, float %22, i32 %13, float %10) nounwind
  store float %23, float* %integral, align 4
  %24 = load i32* %my_rank, align 4
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %bb, label %bb5

bb:                                               ; preds = %entry
  %26 = load float* %integral, align 4
  br label %bb3

bb1:                                              ; preds = %bb3
  %integral2 = bitcast float* %integral to i8*
  %27 = call i32 @MPI_Recv(i8* %integral2, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 %source.0, i32 0, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), %struct.MPI_Status* %status) nounwind
  %28 = load float* %integral, align 4
  %29 = fadd float %total.1, %28
  %30 = add nsw i32 %source.0, 1
  br label %bb3

bb3:                                              ; preds = %bb1, %bb
  %total.1 = phi float [ %26, %bb ], [ %29, %bb1 ]
  %source.0 = phi i32 [ 1, %bb ], [ %30, %bb1 ]
  %31 = load i32* %p, align 4
  %32 = icmp slt i32 %source.0, %31
  br i1 %32, label %bb1, label %bb4

bb4:                                              ; preds = %bb3
  br label %bb7

bb5:                                              ; preds = %entry
  %integral6 = bitcast float* %integral to i8*
  %33 = call i32 @MPI_Send(i8* %integral6, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 0, i32 0, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*)) nounwind
  br label %bb7

bb7:                                              ; preds = %bb5, %bb4
  %total.0 = phi float [ %total.1, %bb4 ], [ undef, %bb5 ]
  %34 = load i32* %my_rank, align 4
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %bb8, label %bb9

bb8:                                              ; preds = %bb7
  %36 = load i32* %n, align 4
  %37 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([38 x i8]* @.str, i32 0, i32 0), i32 %36) nounwind
  %38 = fpext float %total.0 to double
  %39 = load float* %b, align 4
  %40 = fpext float %39 to double
  %41 = load float* %a, align 4
  %42 = fpext float %41 to double
  %43 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([36 x i8]* @.str1, i32 0, i32 0), double %42, double %40, double %38) nounwind
  br label %bb9

bb9:                                              ; preds = %bb8, %bb7
  %44 = call i32 @MPI_Finalize() nounwind
  br label %return

return:                                           ; preds = %bb9
  ret i32 undef
}

declare i32 @MPI_Init(i32*, i8***)

declare i32 @MPI_Comm_rank(%struct.ompi_communicator_t*, i32*)

declare i32 @MPI_Comm_size(%struct.ompi_communicator_t*, i32*)

declare i32 @MPI_Recv(i8*, i32, %struct.ompi_datatype_t*, i32, i32, %struct.ompi_communicator_t*, %struct.MPI_Status*)

declare i32 @MPI_Send(i8*, i32, %struct.ompi_datatype_t*, i32, i32, %struct.ompi_communicator_t*)

declare i32 @printf(i8* noalias, ...) nounwind

declare i32 @MPI_Finalize()

define i32 @Ceiling_log2(i32 %x) nounwind {
entry:
  %"alloca point" = bitcast i32 0 to i32
  %0 = sub i32 %x, 1
  br label %bb1

bb:                                               ; preds = %bb1
  %1 = lshr i32 %temp.0, 1
  %2 = add nsw i32 %result.0, 1
  br label %bb1

bb1:                                              ; preds = %bb, %entry
  %temp.0 = phi i32 [ %0, %entry ], [ %1, %bb ]
  %result.0 = phi i32 [ 0, %entry ], [ %2, %bb ]
  %3 = icmp ne i32 %temp.0, 0
  br i1 %3, label %bb, label %bb2

bb2:                                              ; preds = %bb1
  br label %return

return:                                           ; preds = %bb2
  ret i32 %result.0
}

define i32 @I_receive(i32 %stage, i32 %my_rank, i32* %source_ptr) nounwind {
entry:
  %"alloca point" = bitcast i32 0 to i32
  %0 = shl i32 1, %stage
  %1 = icmp sgt i32 %0, %my_rank
  br i1 %1, label %bb2, label %bb

bb:                                               ; preds = %entry
  %2 = mul nsw i32 %0, 2
  %3 = icmp sle i32 %2, %my_rank
  br i1 %3, label %bb2, label %bb1

bb1:                                              ; preds = %bb
  %4 = sub nsw i32 %my_rank, %0
  store i32 %4, i32* %source_ptr, align 4
  br label %bb3

bb2:                                              ; preds = %bb, %entry
  br label %bb3

bb3:                                              ; preds = %bb2, %bb1
  %.0 = phi i32 [ 0, %bb2 ], [ 1, %bb1 ]
  br label %return

return:                                           ; preds = %bb3
  ret i32 %.0
}

define i32 @I_send(i32 %stage, i32 %my_rank, i32 %p, i32* %dest_ptr) nounwind {
entry:
  %"alloca point" = bitcast i32 0 to i32
  %0 = shl i32 1, %stage
  %1 = icmp slt i32 %my_rank, %0
  br i1 %1, label %bb, label %bb3

bb:                                               ; preds = %entry
  %2 = add nsw i32 %my_rank, %0
  store i32 %2, i32* %dest_ptr, align 4
  %3 = load i32* %dest_ptr, align 4
  %4 = icmp sge i32 %3, %p
  br i1 %4, label %bb1, label %bb2

bb1:                                              ; preds = %bb
  br label %bb4

bb2:                                              ; preds = %bb
  br label %bb4

bb3:                                              ; preds = %entry
  br label %bb4

bb4:                                              ; preds = %bb3, %bb2, %bb1
  %.0 = phi i32 [ 0, %bb1 ], [ 1, %bb2 ], [ 0, %bb3 ]
  br label %return

return:                                           ; preds = %bb4
  ret i32 %.0
}

define void @Send(float %a, float %b, i32 %n, i32 %dest) nounwind {
entry:
  %a_addr = alloca float, align 4
  %b_addr = alloca float, align 4
  %n_addr = alloca i32, align 4
  %"alloca point" = bitcast i32 0 to i32
  store float %a, float* %a_addr
  store float %b, float* %b_addr
  store i32 %n, i32* %n_addr
  %a_addr1 = bitcast float* %a_addr to i8*
  %0 = call i32 @MPI_Send(i8* %a_addr1, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 %dest, i32 0, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*)) nounwind
  %b_addr2 = bitcast float* %b_addr to i8*
  %1 = call i32 @MPI_Send(i8* %b_addr2, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 %dest, i32 1, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*)) nounwind
  %n_addr3 = bitcast i32* %n_addr to i8*
  %2 = call i32 @MPI_Send(i8* %n_addr3, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_int to %struct.ompi_datatype_t*), i32 %dest, i32 2, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*)) nounwind
  br label %return

return:                                           ; preds = %entry
  ret void
}

define void @Receive(float* %a_ptr, float* %b_ptr, i32* %n_ptr, i32 %source) nounwind {
entry:
  %status = alloca %struct.MPI_Status
  %"alloca point" = bitcast i32 0 to i32
  %0 = bitcast float* %a_ptr to i8*
  %1 = call i32 @MPI_Recv(i8* %0, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 %source, i32 0, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), %struct.MPI_Status* %status) nounwind
  %2 = bitcast float* %b_ptr to i8*
  %3 = call i32 @MPI_Recv(i8* %2, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_float to %struct.ompi_datatype_t*), i32 %source, i32 1, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), %struct.MPI_Status* %status) nounwind
  %4 = bitcast i32* %n_ptr to i8*
  %5 = call i32 @MPI_Recv(i8* %4, i32 1, %struct.ompi_datatype_t* bitcast (%struct.ompi_predefined_datatype_t* @ompi_mpi_int to %struct.ompi_datatype_t*), i32 %source, i32 2, %struct.ompi_communicator_t* bitcast (%struct.ompi_predefined_communicator_t* @ompi_mpi_comm_world to %struct.ompi_communicator_t*), %struct.MPI_Status* %status) nounwind
  br label %return

return:                                           ; preds = %entry
  ret void
}

define void @Get_data1(float* %a_ptr, float* %b_ptr, i32* %n_ptr, i32 %my_rank, i32 %p) nounwind {
entry:
  %source = alloca i32
  %dest = alloca i32
  %"alloca point" = bitcast i32 0 to i32
  %0 = icmp eq i32 %my_rank, 0
  br i1 %0, label %bb, label %bb1

bb:                                               ; preds = %entry
  %1 = call i32 @puts(i8* getelementptr inbounds ([18 x i8]* @.str2, i32 0, i32 0)) nounwind
  %2 = call i32 (i8*, ...)* @"\01__isoc99_scanf"(i8* noalias getelementptr inbounds ([9 x i8]* @.str3, i32 0, i32 0), float* %a_ptr, float* %b_ptr, i32* %n_ptr) nounwind
  br label %bb1

bb1:                                              ; preds = %bb, %entry
  br label %bb7

bb2:                                              ; preds = %bb7
  %3 = call i32 @I_receive(i32 %stage.0, i32 %my_rank, i32* %source) nounwind
  %4 = icmp ne i32 %3, 0
  br i1 %4, label %bb3, label %bb4

bb3:                                              ; preds = %bb2
  %5 = load i32* %source, align 4
  call void @Receive(float* %a_ptr, float* %b_ptr, i32* %n_ptr, i32 %5) nounwind
  br label %bb6

bb4:                                              ; preds = %bb2
  %6 = call i32 @I_send(i32 %stage.0, i32 %my_rank, i32 %p, i32* %dest) nounwind
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %bb5, label %bb6

bb5:                                              ; preds = %bb4
  %8 = load i32* %dest, align 4
  %9 = load i32* %n_ptr, align 4
  %10 = load float* %b_ptr, align 4
  %11 = load float* %a_ptr, align 4
  call void @Send(float %11, float %10, i32 %9, i32 %8) nounwind
  br label %bb6

bb6:                                              ; preds = %bb5, %bb4, %bb3
  %12 = add nsw i32 %stage.0, 1
  br label %bb7

bb7:                                              ; preds = %bb6, %bb1
  %stage.0 = phi i32 [ 0, %bb1 ], [ %12, %bb6 ]
  %13 = call i32 @Ceiling_log2(i32 %p) nounwind
  %14 = icmp sgt i32 %13, %stage.0
  br i1 %14, label %bb2, label %bb8

bb8:                                              ; preds = %bb7
  br label %return

return:                                           ; preds = %bb8
  ret void
}

declare i32 @puts(i8*)

declare i32 @"\01__isoc99_scanf"(i8* noalias, ...) nounwind

define float @Trap(float %local_a, float %local_b, i32 %local_n, float %h) nounwind {
entry:
  %"alloca point" = bitcast i32 0 to i32
  %0 = call float @f(float %local_a) nounwind
  %1 = call float @f(float %local_b) nounwind
  %2 = fadd float %0, %1
  %3 = fdiv float %2, 2.000000e+00
  br label %bb1

bb:                                               ; preds = %bb1
  %4 = fadd float %x.0, %h
  %5 = call float @f(float %4) nounwind
  %6 = fadd float %5, %integral.0
  %7 = add nsw i32 %i.0, 1
  br label %bb1

bb1:                                              ; preds = %bb, %entry
  %integral.0 = phi float [ %3, %entry ], [ %6, %bb ]
  %x.0 = phi float [ %local_a, %entry ], [ %4, %bb ]
  %i.0 = phi i32 [ 1, %entry ], [ %7, %bb ]
  %8 = sub nsw i32 %local_n, 1
  %9 = icmp sge i32 %8, %i.0
  br i1 %9, label %bb, label %bb2

bb2:                                              ; preds = %bb1
  %10 = fmul float %integral.0, %h
  br label %return

return:                                           ; preds = %bb2
  ret float %10
}

define float @f(float %x) nounwind {
entry:
  %"alloca point" = bitcast i32 0 to i32
  %0 = fmul float %x, %x
  br label %return

return:                                           ; preds = %entry
  ret float %0
}
